<template>
  <div class="page-container">
    <pf-table
      ref="pfTable"
      v-model="form"
      :columns="columns"
      :form-items="formItems"
      @get-table-data="getTableData"
    >
      <template #handle-area>
        <el-button type="primary" @click="addHandle">新增协议</el-button>
      </template>
      <template #statusStr="{row}">
        <el-tag v-if="row.status === 0" type="success">{{ row.statusStr }}</el-tag>
        <el-tag v-if="row.status === 1" type="danger">{{ row.statusStr }}</el-tag>
      </template>
      <template #handle="{row}">
        <el-button type="text" size="small" @click="showDetail(row)">详情</el-button>
        <el-button type="text" :class="row.status === 0 ? 'danger-text-button': ''" size="small" @click="editStatus(row)">{{ row.status === 0 ? '禁用': '启用' }}</el-button>
      </template>
    </pf-table>
    <AddAgreement
      v-if="showAgreement"
      :id="agreementId"
      :type="type"
      @close="closeHandle"
      @confirm="confirmHandle"
    />
  </div>
</template>

<script>
import { getAgreementList, editAgreementStatus } from '@/api/agreement'
import { getIndustryList } from '@/api/dictionary'
import AddAgreement from './AddAgreement.vue'

export default {
  name: 'Department',
  components: {
    AddAgreement
  },
  data() {
    return {
      type: 'add',
      industryList: [],
      showAgreement: false,
      form: {},
      loading: false,
      agreementId: ''
    }
  },
  computed: {
    columns() {
      const attrs = { 'min-width': '150px' }
      return [
        { label: '协议编号', prop: 'agreementNo', attrs },
        { label: '协议名称', prop: 'agreementName', attrs },
        { label: '行业', prop: 'industryName', attrs },
        { label: '协议类型', prop: 'agreementTypeStr', attrs },
        { label: '创建时间', prop: 'createTime', attrs },
        { label: '状态', slot: 'statusStr', attrs },
        { label: '操作', slot: 'handle', attrs: { width: '150px', fixed: 'right' }}
      ]
    },
    formItems() {
      const labelWidth = '85px'
      return [
        { type: 'input', label: '协议编号', prop: 'agreementNo', formItemAttrs: { labelWidth }},
        { type: 'input', label: '协议名称', prop: 'agreementName', formItemAttrs: { labelWidth }},
        {
          type: 'select',
          label: '所属行业',
          prop: 'industryCode',
          options: this.industryList,
          optProps: {
            label: 'industryName',
            value: 'industryCode'
          },
          formItemAttrs: { labelWidth }
        },
        {
          type: 'date',
          label: '创建时间',
          prop: 'dateRange',
          span: 8,
          attrs: {
            type: 'datetimerange'
          },
          formItemAttrs: { labelWidth }
        }
      ]
    }
  },

  created() {
    this.getIndustryList()
  },

  methods: {

    getIndustryList() {
      getIndustryList().then(res => {
        this.industryList = res || []
      }).catch(() => {})
    },

    getTableData({ ...formQuery }, done) {
      this.loading = true
      const [createStartTime = '', createEndTime = ''] = formQuery?.dateRange || []
      const data = {
        createStartTime,
        createEndTime,
        ...formQuery
      }
      getAgreementList(data).then(res => {
        this.loading = false
        done({ data: res.data || [], total: res.count || 0 })
      }).catch(() => {
        done({ data: [], total: 0 })
        this.loading = false
      })
    },

    // 新增
    addHandle() {
      this.type = 'add'
      this.agreementId = ''
      this.showAgreement = true
    },

    // 查看详情
    showDetail(row) {
      this.type = 'detail'
      this.agreementId = row.id
      this.showAgreement = true
    },

    // 修改协议状态
    editStatus(row) {
      let status = ''
      let message = ''
      if (row.status === 0) {
        message = '确定禁用该协议吗'
        status = 1
      } else {
        message = '确定启用该协议吗'
        status = 0
      }
      const data = {
        id: row.id,
        status
      }
      this.$confirm(message, '提示', {
        type: 'warning'
      }).then(() => {
        editAgreementStatus(data).then(() => {
          this.$message.success('修改成功')
          this.$refs.pfTable.getTableData()
        }).catch(() => {})
      }).catch(() => {})
    },

    // 关闭弹窗
    closeHandle() {
      this.showAgreement = false
    },

    // 确定
    confirmHandle() {
      this.$refs.pfTable.getTableData()
    }
  }
}
</script>

<style lang="scss" scoped></style>
